<?php

Yii::import('Cms.controllers.LanguageContentController');
class LanguageSelectorController extends LanguageContentController implements ICmsWidget
{
    public $info = array(
        'path'=>'Cms/widgets/languageSelector',
        'name'=>'Language Selector Widget',
        'description'=>'Language Selector Widget',
        'images'=>'',
        'video'=>'',
        'author'=>'',
        'version'=>1,
    );
    
    public function getDefaultConfig()
    {
        return CMap::mergeArray(parent::getDefaultConfig(), array(

        ));
    }
    
    public function __construct($id,$module=null)
    {
        //new instance of widget => load widget settings
        $this->getWidgetSettings();
        parent::__construct($id,$module);
    }
    
    public function actionInstall()
    {
        $this->installWidget();
    }
    
    public function actionUninstall()
    {
        $this->uninstallWidget();
    }
    
    public function actionConfig()
    {
        $this->widgetConfig();
    }
    
    public function actionIndex()
    {
        $localeIds = array();
        $result = FSM::run('Cms.Site.getSupportedLanguages');        
        if (!$result->hasErrors())
        {
            $localeIds = $result->localeID;
        }
        $this->render($this->getWidgetLayout()->getViewFile(),array('localeIds'=>$localeIds,'selectedLocaleId'=>Yii::app()->locale->id));
    }
    
    public function actionUpdate()
    {
        $formParams = $this->getFormParams();
        $formLayoutParams = $this->getFormLayoutParams();
        
        $widgetSetting = array();
        if (Yii::app()->request->IsPostRequest) {
            $this->message = Yii::t('Core','Item has been saved successfully.');
            //save widget settings
            $widgetSetting['widget'] = $formParams->saveParams($_POST, ParamForm::TO_ARRAY);
            //save widget layout settings
            $widgetSetting['layout'] = $formLayoutParams->saveParams($_POST, ParamForm::TO_ARRAY);
            if (parent::updateWidgetSettings($widgetSetting))
            {
                if (isset($_POST['continue']))
                    $this->refresh();
                else
                {
                    //close fancybox, reload page
                    Yii::app()->clientScript->registerScript('ReloadPage', "parent.window.location = parent.window.location;");
                }
            }
        }
                
        $this->render('form', array(
            'formParams' => $formParams,
            'formLayoutParams' => $formLayoutParams,
        ));
    }
    
    public function getFlagImageUrl($locale_id)
    {
        $allowedFileExts = array('png','jpg','gif');
        foreach($allowedFileExts as $ext)
        {
            $filePath = Yii::getPathOfAlias('site').'/../themes/'.Yii::app()->theme->name.'/images/language-flags/'.$locale_id.'.'.$ext;
            if (file_exists($filePath))
                return themeUrl().'/images/language-flags/'.$locale_id.'.'.$ext;
        }
        return '';
    }
}